package com.kamikaze;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Modification {
	String user_id ;
	String user_pw ;
	String user_name ;
	String user_gender;
	String user_birthday;
	String user_employee;
	String user_tel;
	String user_mobile;
	String user_address;
	String user_salary;
	String dept_number;
	String user_level;
	String user_image;
	ArrayList<String> errors = new ArrayList<String>();
	boolean update;
	String sql_url = "jdbc:mysql://j.snpy.org/j2?user=team2&password=kamikaze&useUnicode=true&characterEncoding=UTF-8";

	public Modification() {

	}

	

	public Modification(String user_pw, String user_name,
			String user_gender, String user_birthday, String user_employee,
			String user_tel, String user_mobile, String user_address,
			String user_salary, String dept_number, String user_level,
			String user_image) {
		super();
		this.user_pw = user_pw;
		this.user_name = user_name;
		this.user_gender = user_gender;
		this.user_birthday = user_birthday;
		this.user_employee = user_employee;
		this.user_tel = user_tel;
		this.user_mobile = user_mobile;
		this.user_address = user_address;
		this.user_salary = user_salary;
		this.dept_number = dept_number;
		this.user_level = user_level;
		this.user_image = user_image;
		this.errors = errors;
	}
	public boolean updateall(){
		boolean update = true;
		if (user_name == null && user_name.length() < 2) {
			errors.add("請輸入姓名");
			update = false;
		}
		// 密碼驗證
		if (!user_pw.matches("[a-zA-Z0-9]{8,12}")) {
			errors.add("密碼需為英數混合之八碼");
			update = false;
		}
		// 生日驗證
		if (!user_birthday
				.matches("([20]|[19]){2}[0-3]{1}[0-9]{1}-[0-1]{1}[0-2]{1}-[0-3]{1}[0-9]{1}")) {
			errors.add("生日格式需為西元 年、月、日");
			update = false;
		}
		// 任用日期
		try {
			Date date = new Date();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date today = df.parse(df.format(date));
			if (!user_employee
					.matches("([20]|[19]){2}[0-3]{1}[0-9]{1}-[0-1]{1}[0-2]{1}-[0-3]{1}[0-9]{1}")) {
				errors.add("任用日期格式需為西元 年、月、日");
				update = false;
			}
			java.sql.Date u_emp = java.sql.Date.valueOf(user_employee);
			if (u_emp.before(today)) {
				errors.add("任用日期不能在今天之前");
				update = false;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 電話驗證
		if (!user_tel.matches("[0-9]{4}-[0-9]{4}")) {
			errors.add("請輸入格式為xxxx-xxxx之電話號碼");
			update = false;
		}
		// 手機驗證
		if (!user_mobile.matches("[0]{1}[9]{1}[0-9]{2}-[0-9]{3}-[0-9]{3}")) {
			errors.add("請輸入格式為09xx-xxx-xxx之電話號碼");
			update = false;
		}
		// 地址驗證
		if (!user_address.matches("[縣市][路街道][0-9]{1,4}[號]")) {
			errors.add("請輸格式為xx市(縣)xx路(街、道)xx號");
			update = false;
		}
	/*	// 相片路徑驗證
		if (user_image.matches("")) {
			errors.add("請輸入路徑");
			passed = false;
		}
	*/	
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(sql_url);
			PreparedStatement pstmt = con
					.prepareStatement("update users set user_tel=?, user_mobile=?, user_address=?,");
			pstmt.setString(1, user_pw);
			pstmt.setString(2, user_name);
			pstmt.setString(3, user_gender);
			pstmt.setString(4, user_birthday);
			pstmt.setString(5, user_employee);
			pstmt.setString(6, user_tel);
			pstmt.setString(7, user_mobile);
			pstmt.setString(8, user_address);
			pstmt.setString(9, user_salary);
			pstmt.setString(10, dept_number);
			pstmt.setString(11, user_level);
			pstmt.setString(12, user_image);
			pstmt.executeUpdate();
			pstmt.close();
			con.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return update;
		
	}


	public Modification(String user_tel, String user_mobile, String user_address) {
		this.user_tel = user_tel;
		this.user_mobile = user_mobile;
		this.user_address = user_address;
	}

	public boolean update() {

		update = true;
		// 驗證電話
		if (!user_tel.matches("[0-9]{4}-[0-9]{4}")) {
			errors.add("請輸入格式為xxxx-xxxx之電話號碼");
			update = false;
		}
		// 驗證手機
		if (!user_mobile.matches("[0]{1}[9]{1}[0-9]{2}-[0-9]{3}-[0-9]{3}")) {
			errors.add("請輸入格式為09xx-xxx-xxx之電話號碼");
			update = false;
		}
		// 驗證地址
		if (!user_address.matches("[縣市][路街道][0-9]{1,4}[號]")) {
			errors.add("請輸格式為xx市(縣)xx路(街、道)xx號");
			update = false;
		}
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(sql_url);
			PreparedStatement pstmt = con
					.prepareStatement("update users set user_tel=?, user_mobile=?, user_address=?");
			pstmt.setString(1, user_tel);
			pstmt.setString(2, user_mobile);
			pstmt.setString(3, user_address);
			pstmt.executeUpdate();
			pstmt.close();
			con.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return update;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getUser_gender() {
		return user_gender;
	}

	public void setUser_gender(String user_gender) {
		this.user_gender = user_gender;
	}

	public String getUser_tel() {

		return user_tel;
	}

	public void setUser_tel(String user_tel) {
		this.user_tel = user_tel;
	}

	public String getUser_mobile() {
		return user_mobile;
	}

	public void setUser_mobile(String user_mobile) {
		this.user_mobile = user_mobile;
	}

	public String getUser_address() {
		return user_address;
	}

	public void setUser_address(String user_address) {
		this.user_address = user_address;
	}

	public String getUser_salary() {
		return user_salary;
	}

	public void setUser_salary(String user_salary) {
		this.user_salary = user_salary;
	}

	public String getDept_number() {
		return dept_number;
	}

	public void setDept_number(String dept_number) {
		this.dept_number = dept_number;
	}

	public String getUser_level() {
		return user_level;
	}

	public void setUser_level(String user_level) {
		this.user_level = user_level;
	}

}
